import { app, Tray, Menu, clipboard, BrowserWindow, globalShortcut, dialog } from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`


// const appIcon = new Tray(__static+'/images/favicon.png')
function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
      height: 750,
      width: 1360,
      useContentSize: true,
      minWidth: 305,
      minHeight: 215,
      frame: false,
      webSecurity: false,
      webPreferences: {
          allowRunningInsecureContent: true,
          webSecurity: false
      },
      icon: __static+'/images/favicon.png'
  })

  mainWindow.loadURL(winURL)
  setMenus()
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', () => {

    // 键盘快捷键
    /*const ret = globalShortcut.register('CommandOrControl+X', () => {
        dialog.showMessageBox({
            message: 'CommandOrControl+X is pressed'
        })
    })

    if (!ret) {
        console.log('registration failed')
    }

    // 检查快捷键是否注册成功

    console.log(globalShortcut.isRegistered('CommandOrControl+X'))

    dialog.showMessageBox({
        message: globalShortcut.isRegistered('CommandOrControl+X') ? 'ok' : 'no'
    })*/

    createWindow();
})

app.on('will-quit', function () {
    globalShortcut.unregisterAll()
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */


/* 添加图标和上下文菜单到系统通知区 */

let tray = null
function setMenus () {
    tray = new Tray(__static+'/images/favicon.png')
    const contextMenu = Menu.buildFromTemplate([
            {
                label: 'Edit',
                // icon: __static+'/images/favicon.png',
                sublabel: '6666',
                submenu: [
                    {role: 'undo'},
                    {role: 'redo'},
                    {type: 'separator'},
                    {role: 'cut'},
                    {role: 'copy'},
                    {role: 'paste'},
                    {role: 'pasteandmatchstyle'},
                    {role: 'delete'}
                ]
            },

        ]
    )
    tray.setToolTip('视频播放器')
    tray.setContextMenu(contextMenu)
}